/**
 * 
 */
package com.acc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.acc.softmgr.business.model.SoftmgrSystem;
import com.acc.softmgr.business.model.SoftmgrSystemExample;
import com.acc.softmgr.business.model.SysUser;
import com.acc.softmgr.business.service.ISoftmgrProjectService;
import com.acc.softmgr.business.service.ISoftmgrSystemService;
import com.acc.softmgr.common.constant.DeleteStatus;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.utils.date.DateBase;
import com.acc.softmgr.common.utils.string.StringUtil;
import com.acc.softmgr.common.web.DataGridObject;
import com.acc.softmgr.common.web.ResponseObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("softSystem")
public class SoftmgrSystemController {

	@Autowired
	private ISoftmgrProjectService softmgrProjectService;

	@Autowired
	private ISoftmgrSystemService softmgrSystemService;

	@RequestMapping(value = "toSoftSystem", method = RequestMethod.GET)
	public String toSoftSystem() {
		return "softSystem/softSystem";
	}

	/**
	 * 查询列表页
	 * 
	 * @param project_name
	 * @param project_english
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSoftSystem", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SoftmgrSystem> searchSoftSystem(@Validated String project_id, @Validated String system_name,
			@Validated String system_english, Integer page, Integer rows, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DataGridObject<SoftmgrSystem> responseObject = new DataGridObject<SoftmgrSystem>();
		SoftmgrSystemExample example = new SoftmgrSystemExample();
		if (!StringUtil.isEmpty(project_id)) {
			example.getCriteria().andProject_idEqualTo(Integer.valueOf(project_id));
		}
		if (!StringUtil.isEmpty(system_name)) {
			example.getCriteria().andSystem_nameLike("%" + system_name + "%");
		}
		if (!StringUtil.isEmpty(system_english)) {
			example.getCriteria().andSystem_englishLike("%" + system_english + "%");
		}
		if (page == null || page == 0) {
			page = 1;
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SoftmgrSystem> softmgrSystems = softmgrSystemService.selectByExample(example);
		for (SoftmgrSystem softmgrSystem : softmgrSystems) {
			softmgrSystem.setCreateTimestring(
					DateBase.formatDate(softmgrSystem.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
			softmgrSystem.setProjectName(
					softmgrProjectService.selectByPrimaryKey(softmgrSystem.getProject_id()).getProject_name());
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(softmgrSystems);
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSoftSystem", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject addSoftSystem(@Validated String project_id, String system_name, String system_english,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(project_id) || StringUtil.isEmpty(system_name) || StringUtil.isEmpty(system_english)) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");

		SoftmgrSystem SoftmgrSystem = new SoftmgrSystem();
		SoftmgrSystem.setCreatetime(new Date());
		SoftmgrSystem.setCreateby(currentUser.getUsername());
		SoftmgrSystem.setProject_id(Integer.valueOf(project_id));
		SoftmgrSystem.setSystem_name(system_name);
		SoftmgrSystem.setSystem_english(system_english);
		SoftmgrSystem.setDeleted(DeleteStatus.FALSE);
		// -------------------
		boolean exitSystem = exitSoftmgrSystem(project_id, system_name, system_english);

		if (exitSystem) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		softmgrSystemService.insert(SoftmgrSystem);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSoftSystem", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject editSoftSystem(@Validated Integer id, String project_id, String system_name,
			String system_english, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(project_id) || StringUtil.isEmpty(system_name) || StringUtil.isEmpty(system_english)
				|| id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrSystem softmgrSystem = softmgrSystemService.selectByPrimaryKey(id);
		boolean exitSystem = exitSoftmgrSystem(project_id, system_name, system_english);
		if (exitSystem) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		softmgrSystem.setProject_id(Integer.valueOf(project_id));
		softmgrSystem.setSystem_name(system_name);
		softmgrSystem.setSystem_english(system_english);
		softmgrSystemService.updateByPrimaryKey(softmgrSystem);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSoftSystem", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchUser(@PathVariable Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrSystem SoftmgrSystem = softmgrSystemService.selectByPrimaryKey(id);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(SoftmgrSystem);
		return responseObject;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSoftSystem", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject removeSoftSystem(@Validated Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrSystem softmgrSystem = softmgrSystemService.selectByPrimaryKey(id);
		softmgrSystem.setDeleted(DeleteStatus.TRUE);
		softmgrSystemService.updateByPrimaryKey(softmgrSystem);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 获取制定Project下面的system
	 * 
	 * @param projectId
	 * @return
	 */
	@RequestMapping(value = "getSoftSystem/{projectId}", method = RequestMethod.GET)
	@ResponseBody
	public String getSoftProjects(@PathVariable Integer projectId) throws Exception {

		SoftmgrSystemExample example = new SoftmgrSystemExample();
		example.getCriteria().andProject_idEqualTo(projectId);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrSystem> list = softmgrSystemService.selectByExample(example);
		JSONArray jsonArray = new JSONArray();
		for (SoftmgrSystem system : list) {
			JSONObject json = new JSONObject();
			json.put("id", system.getId());
			json.put("text", system.getSystem_name());
			jsonArray.add(json);
		}

		return jsonArray.toJSONString();
	}

	private boolean exitSoftmgrSystem(String project_id, String project_name, String project_english) {
		SoftmgrSystemExample example = new SoftmgrSystemExample();
		example.getCriteria().andProject_idEqualTo(Integer.valueOf(project_id));
		example.getCriteria().andSystem_nameEqualTo(project_name);
		example.getCriteria().andSystem_englishEqualTo(project_english);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrSystem> softmgrSystems = softmgrSystemService.selectByExample(example);
		if (softmgrSystems != null && softmgrSystems.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

}
